<?php

namespace StarCloud\Library\Sdk;

use Framework\DiServices\HttpClient;
use Phalcon\Di;
use StarCloud\Library\Exception;

class NovelShort
{
    protected $sea_pid = 53;
    protected $sea_token = '4d4d96030e92f477b78a2bead834722e66bc91b63aa64';

    /* @var $httpClient HttpClient */
    private $httpClient = null;
    private $cacheClass;

    protected $domain = 'https://app.yaozhiyu.cn/v2/sea';

    const plats_map = [
        '166' => ['plat' => 'KISS','type' => 1],
        '167' => ['plat' => 'PickNovel','type' => 1],
        '168' => ['plat' => 'MoboReader','type' => 1],
        '169' => ['plat' => 'NovaBeats','type' => 1],
        '165' => ['plat' => 'ReelShort','type' => 2],
        '163' => ['plat' => 'Novel Master','type' => 1],
        '164' => ['plat' => 'Good Novel','type' => 1],
        '157' => ['plat' => 'Novel Short','type' => 1],
    ];
    const plats = [
        'KISS' => ['app_id' => 166,'type' => 1],
        'PickNovel' => ['app_id' => 167,'type' => 1],
        'MoboReader' => ['app_id' => 168,'type' => 1],
        'NovaBeats' => ['app_id' => 169,'type' => 1],
        'Novel Master' => ['app_id' => 163,'type' => 1],
        'ReelShort' => ['app_id' => 165,'type' => 2],
        'Good Novel' => ['app_id' => 164,'type' => 1],
        'Novel Short' => ['app_id' => 157,'type' => 1],
    ];

    public function __construct()
    {
        $this->httpClient = Di::getDefault()->get('httpClient');
        $this->cacheClass = Di::getDefault()->get('redis');
    }

    public function getHeader()
    {
        return [
            'Content-Type' => 'application/json',
            'sea-token' =>  $this->sea_token,
            'sea-pid' =>  $this->sea_pid,
        ];
    }

    public function getRefundOrder($startTime, $endTime, $pageNum = 1, $size = 10)
    {
        $url = $this->domain . "/refundList?startTime={$startTime}&endTime={$endTime}&pageNum={$pageNum}&pageSize={$size}";

        $response = $this->httpClient->sendGetRequest($url, [], $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        if (isset($json['code']) && $json['code'] == 0) {
            return $json['data'];
        }
        return [];
    }
    public function getBookList($book_name, $type, $plate, $page, $size)
    {
        $url = $this->domain . "/bookList?book_name={$book_name}&type={$type}&plate={$plate}&pageNum={$page}&pageSize={$size}";

        $response = $this->httpClient->sendGetRequest($url, [], $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        Di::getDefault()->get("logger")->info("novel_short_book_list: " . json_encode($json));

        if (isset($json['code']) && $json['code'] == 0) {
            foreach ($json['data'] as &$item) {
                $item['content'] = $this->getBookInfo($item['id'], $plate);
            }
            return $json['data'];
        }
        return [];
    }

    public static function getBookKey($bookId)
    {
        return "novel_short_book_{$bookId}";
    }


    public function getBookInfo($book_id, $plate, $isCache = false)
    {
        $url = $this->domain ."/bookInfo?contentId={$book_id}&plate={$plate}&book_id={$book_id}";

        $response = $this->httpClient->sendGetRequest($url, [], $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);

        Di::getDefault()->get("logger")->info("novel_short_book: " . json_encode($json));
        if (isset($json['code']) && $json['code'] == 0) {
            return $json['data'];
        }
        return '';
    }

    public function createKeyword($book_id, $keyword, $plate_name)
    {
        $url = $this->domain ."/createKeywords";

        $body = [
            'bookId' => $book_id,
            'customParams' => $keyword,
            'plate_name' => $plate_name
        ];


        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        Di::getDefault()->get("logger")->info("novel_short_body {$url}" . json_encode($body));

        if (isset($json['code']) && $json['code'] == 0) {
            return [$json['data']['code'], $json['data']['promotion_id']];
        }

        throw new Exception(9999, $json['msg'] ?? "系统错误");
    }

    public function getOrders($startTime, $endTime, $page, $size)
    {
        $url = $this->domain ."/orderList?startTime={$startTime}&endTime={$endTime}&pageNum={$page}&pageSize={$size}";

        $response = $this->httpClient->sendGetRequest($url, [], $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        if (isset($json['code']) && $json['code'] == 0) {
            return $json['data'];
        }
        return [];
    }
}